

import UIKit
import GradientLoadingBar
import Alamofire
import Reachability
import SwiftEventBus


class CompletionViewController: UIViewController {
    let cell_identifier:String = "completionCell"
    var list:[Completion] = []
    var myNavigationController: UINavigationController?
    var isFavorite = false
    
    var showDeleteButton = false
    // 懒加载
    lazy var tableView : UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        
        let itemW = (UIScreen.main.bounds.width - 32)/2
        layout.itemSize = CGSize(width: itemW, height: itemW)
        
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        layout.minimumLineSpacing = 16
        layout.minimumInteritemSpacing = 10
        
        layout.scrollDirection = .vertical // 竖向滑动
        
        
        let tableView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        tableView.showsVerticalScrollIndicator = false
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.register(CompletionCell.self, forCellWithReuseIdentifier: cell_identifier)
        
        return tableView
    }()
    override var wx_titleTextAttributes: [NSAttributedString.Key : Any]? {
           return [NSAttributedString.Key.foregroundColor: UIColor.black]
       }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "创作"
        self.view.addSubview(tableView)
        self.view.backgroundColor = .white
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(self.view).offset(32)
            make.bottom.equalTo(self.view)
            make.width.equalTo(self.view)
        }
        
        
    }
    
    
   
    
}

extension CompletionViewController: UICollectionViewDataSource,UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return list.count
    }
    
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cell_identifier, for: indexPath) as! CompletionCell
        cell.bindViewModel(list[indexPath.row],showDeleteButton)
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath.item + 1)
        let completion = list[indexPath.row]
        if isFavorite{
            self.view.makeToast("收藏成功",position: .center)
            SwiftEventBus.post(EVENT_NAME_ADD_COMPLETION, sender: completion)
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.6){
                self.myNavigationController?.popViewController(animated: true)
            }
        }else{
           
            
            if completion.type == 1{
                let detailViewController = CompletionImageViewController()
                detailViewController.pageTitle = completion.title
                detailViewController.type = completion.type
                detailViewController.rolePrompt = completion.prompt
                self.myNavigationController?.pushViewController(detailViewController, animated: true)
                
            }else{
                let detailViewController = CompletionTextViewController()
                detailViewController.pageTitle = completion.title
                detailViewController.type = completion.type
                detailViewController.prompt = completion.prompt
                detailViewController.role = completion.role
                detailViewController.menu = completion.menu
                detailViewController.menu2 = completion.menu2
                detailViewController.menu3 = completion.menu3
                detailViewController.example = completion.example
                detailViewController.answer = completion.answer
                
                self.myNavigationController?.pushViewController(detailViewController, animated: true)
            }
        }
    }
    
}
